package com.xiong.util;

public class UnicodeUtils {

    public static void main(String[] args) {
        String str=chineseToUnicode("你好");
        String s=unicodeToChinese("\\u6240\\u586b\\u5199\\u8bbe\\u5907\\u72b6\\u6001\\u4e0d\\u5b58\\u5728");
        System.out.println(s);

    }

    public static String chineseToUnicode(String chineseStr){
        StringBuilder unicode = new StringBuilder();
        for (char c : chineseStr.toCharArray()) {
            unicode.append("\\u").append(Integer.toHexString(c | 0x10000).substring(1));
        }
        System.out.println(unicode.toString());
        return unicode.toString();
    }

    public static String unicodeToChinese(String unicode){
        StringBuilder chinese = new StringBuilder();
        String[] hex = unicode.split("\\\\u");
        for (int i = 1; i < hex.length; i++) {
            int data = Integer.parseInt(hex[i], 16);
            chinese.append((char) data);
        }
        return chinese.toString();
    }
}
